<template>
    <!-- weixinmao_hssy/pages/user/index.wxml -->
    <view class="container">
        <view class="header" style="background: #2577f5">
            <view>
                <view class="pic">
                    <image :src="userinfo.avatarUrl"></image>
                    <view class="userinfo">
                        <view>{{ userinfo.nickname }}</view>
                    </view>
                </view>
            </view>
        </view>

        <view class="noteoper">
            <view class="money">￥{{ totalmoney }}</view>
            <view class="getmoney"><button class="cu-btn round bg-yellow shadow" style="color: #ffffff; margin-left: 15rpx" @tap="toGetmoney">申请提现</button></view>

            <view class="notenav">
                <button class="cu-btn round bg-blue shadow" @tap="toMymoneyrecord">佣金明细</button>
                <button class="cu-btn round bg-blue shadow" @tap="toMyteam">我的团队</button>
                <button class="cu-btn round bg-blue shadow" @tap="formSubmit">我的二维码</button>
                <!-- <button class="cu-btn round bg-olive shadow"  bindtap="toMyagentqrcode">我的二维码</button> -->
            </view>
        </view>

        <view class="list">
            <view class="item" @tap="toMytasklist">
                <image src="/static/imgs/icon/msgorder.png"></image>
                <view style="margin-left: 16rpx">我的任务</view>
            </view>

            <view class="item" @tap="toSendorder">
                <image src="/static/imgs/icon/matchorder.png"></image>
                <view style="margin-left: 16rpx">派遣订单</view>
            </view>

            <view class="item" @tap="toAgentnotelist">
                <image src="/static/imgs/icon/matchorder.png"></image>
                <view style="margin-left: 16rpx">我的人才库</view>
            </view>

            <view class="item" @tap="toAgentcompanylist">
                <image src="/static/imgs/icon/qorder.png"></image>
                <view style="margin-left: 16rpx">推荐企业</view>
            </view>

            <view class="item" @tap="toAgentFxrecord">
                <image src="/static/imgs/icon/qorder.png"></image>
                <view style="margin-left: 16rpx">推广用户记录</view>
            </view>
        </view>

        <view class="imagePathBox" v-if="maskHidden != false">
            <image :src="imagePath" class="shengcheng"></image>
            <button class="baocun" @tap="baocun">保存相册，分享到朋友圈</button>
        </view>

        <view v-if="maskHidden != false" class="mask"></view>
        <view class="canvas-box">
            <canvas style="width: 375px; height: 667px; position: fixed; top: 9999px" canvas-id="mycanvas" />
        </view>
    </view>
</template>

<script>
// pages/user/index.js
import { Token } from '../../utils/token.js';
import { My } from '../my/my-model.js';
var my = new My();
import { Agent } from '../../model/agent-model.js';
var agent = new Agent();
export default {
    data() {
        return {
            isuser: true,

            /* 生成二维码参数 */
            img: '/static/imgs/icon/gobg.jpg',

            wechat: '../../imgs/icon/wechat.png',
            quan: '../../imgs/icon/quan.png',
            code: 'E7AI98',
            inputValue: '',
            maskHidden: false,
            name: '',
            touxiang: '',

            code: 'E7A93C',
            /* 生成二维码参数 */ totalmoney: '',

            userinfo: {
                avatarUrl: '',
                nickname: ''
            },

            imagePath: '',
            canvasHidden: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        var that = this;
        uni.setNavigationBarTitle({
            title: '经纪人中心'
        });
        var appuser = uni.getStorageSync('userInfo');
        agent.agentInit((data) => {
            that.myqrcode = data.agentinfo.qrcode;
            that.usermoney = data.usermoney;
            that.setData({
                totalmoney: data.totalmoney
            });
        });
        if (!appuser) {
            that.isuser = true;
        } else {
            that.isuser = false;
            that.setData({
                userinfo: appuser
            });
        }
        that.setData({
            isuser: that.isuser
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        toGetmoney: function () {
            var that = this;
            if (that.usermoney <= 0) {
                uni.showModal({
                    title: '提示',
                    content: '余额不足,无法提现',
                    showCancel: false
                });
                return;
            } else {
                uni.showModal({
                    title: '提示',
                    content: '确认提现？',
                    success: function (res) {
                        if (res.confirm) {
                            var params = {};
                            agent.getUserMoney((data) => {
                                if (data.error == 0) {
                                    uni.showModal({
                                        title: '提示',
                                        content: '提现成功',
                                        showCancel: false,
                                        success: function () {
                                            uni.navigateTo({
                                                url: '/pages/mymoneyrecord/index'
                                            });
                                        }
                                    });
                                } else {
                                    uni.showModal({
                                        title: '提示',
                                        content: data.msg,
                                        showCancel: false
                                    });
                                    return;
                                }
                            }, params);
                        }
                    }
                });
            }
        },

        createNewImg: function () {
            var that = this;
            var context = uni.createCanvasContext('mycanvas');
            context.setFillStyle('#ffe200');
            context.fillRect(0, 0, 375, 667);
            var path = '/static/imgs/icon/gobg.jpg';
            //将模板图片绘制到canvas,在开发工具中drawImage()函数有问题，不显示图片
            //不知道是什么原因，手机环境能正常显示
            context.drawImage(path, 0, 0, 375, 667);
            var path1 = that.touxiang;
            console.log(path1, 'path1');
            //将模板图片绘制到canvas,在开发工具中drawImage()函数有问题，不显示图片
            //var path2 = "../../imgs/icon/txquan.png";
            //var path3 = "../../imgs/icon/heise.png";

            var path4 = '/static/imgs/icon/wenziBg.png';
            var path5 = '../../imgs/icon/wenxin.png';

            // context.drawImage(path2, 126, 186, 120, 120);

            var name = that.name;
            //绘制名字
            context.setFontSize(24);
            context.setFillStyle('#333333');
            context.setTextAlign('center');
            context.fillText(name, 185, 340);
            context.stroke();

            //绘制左下角文字背景图
            context.drawImage(path4, 25, 520, 184, 82);
            context.setFontSize(12);
            context.setFillStyle('#333');
            context.setTextAlign('left');
            context.fillText('进入小程序查看海量职位', 35, 540);
            context.stroke();
            context.setFontSize(12);
            context.setFillStyle('#333');
            context.setTextAlign('left');
            context.fillText(',高端职位正在等着', 35, 560);
            context.stroke();
            context.setFontSize(12);
            context.setFillStyle('#333');
            context.setTextAlign('left');
            context.fillText('你来哦~', 35, 580);
            context.stroke();
            //绘制右下角扫码提示语

            context.drawImage(that.myqrcodefile, 243, 495, 100, 100);
            context.drawImage(path5, 248, 598, 90, 25);
            //绘制头像
            context.arc(186, 246, 50, 0, 2 * Math.PI); //画出圆
            context.strokeStyle = '#ffe200';
            context.clip(); //裁剪上面的圆形
            context.drawImage(path1, 136, 196, 100, 100); // 在刚刚裁剪的园上画图
            context.draw();
            //将生成好的图片保存到本地，需要延迟一会，绘制期间耗时

            setTimeout(function () {
                uni.hideToast();
                that.drawAfter();
                that.setData({
                    maskHidden: true
                });
            }, 200);
        },

        //点击保存到相册
        baocun: function () {
            var that = this;
            uni.saveImageToPhotosAlbum({
                filePath: that.imagePath,
                success(res) {
                    uni.showModal({
                        content: '图片已保存到相册，赶紧晒一下吧~',
                        showCancel: false,
                        confirmText: '好的',
                        confirmColor: '#333',
                        success: function (res) {
                            if (res.confirm) {
                                console.log('用户点击确定');
                                /* 该隐藏的隐藏 */
                                that.setData({
                                    maskHidden: false
                                });
                            }
                        },
                        fail: function (res) {
                            console.log(11111);
                        }
                    });
                }
            });
        },

        //点击生成
        formSubmit: function (e) {
            var that = this;
            this.setData({
                maskHidden: false
            });
            uni.showToast({
                title: '正在生成中...',
                icon: 'loading',
                duration: 1000
            });

            // that.data.myqrcode = 'https://dev.site100.cn/uploads/images/1640250895qrcode.jpg';
            uni.downloadFile({
                url: that.myqrcode,
                success: function (res) {
                    console.log(res.tempFilePath);
                    that.myqrcodefile = res.tempFilePath;
                    that.createNewImg();
                }
            });
        },

        drawAfter: function () {
            var that = this;
            uni.canvasToTempFilePath({
                canvasId: 'mycanvas',
                success: function (res) {
                    var tempFilePath = res.tempFilePath;
                    that.setData({
                        imagePath: tempFilePath,
                        canvasHidden: true
                    });
                },
                fail: function (res) {
                    console.log(res);
                }
            });
        },

        bindGetUserInfo: function () {
            var that = this;
            var token = new Token();
            token.islogin = 1;
            token.verify((data) => {
                console.log(data);
                uni.setStorageSync('userInfo', data);
                that.isuser = false;
                that.setData({
                    isuser: that.isuser,
                    userinfo: data
                });
            });

            /*
         my.getUserInfo((data)=>{
      
          console.log(data);
      
          wx.setStorageSync('userInfo',data);
          that.data.isuser = false;
             that.setData({
            
                isuser:that.data.isuser,
                 userinfo:data
             });
      
         }
         
         );
      
         */
        },

        toSendorder: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/sendorder/index'
            });
        },

        toMyNote: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/mynote/index'
            });
        },

        toMyagentqrcode: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/myagentqrcode/index'
            });
        },

        toMyFind: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/myfind/index'
            });
        },

        toAgentnotelist: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/agentnotelist/index'
            });
        },

        toAgentcompanylist: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/agentcompanylist/index'
            });
        },

        toAgentFxrecord: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/agentfxrecord/index'
            });
        },

        toMytasklist: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/mytasklist/index'
            });
        },

        toMySave: function (e) {
            var that = this;
            uni.navigateTo({
                url: '/pages/mysave/index'
            });
        },

        toMyteam: function (e) {
            var that = this;
            uni.navigateTo({
                url: '/pages/myteam/index'
            });
        },

        toMymoneyrecord: function () {
            var that = this;
            uni.navigateTo({
                url: '/pages/mymoneyrecord/index'
            });
        },

        toMyGz: function (e) {
            var that = this;
            uni.navigateTo({
                url: '/pages/mygz/index'
            });
        }
    }
};
</script>
<style>
page {
    background: #f5f5f5;
}
.container {
    background-color: #f5f5f5;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.container .header {
    width: 100%;
    height: 300rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.container .header .pic {
    width: 100%;
    height: 250rpx;
    align-self: center;
    display: flex;
    flex-direction: row;
    align-items: center;
    padding-left: 20rpx;
}
.container .header .pic image {
    width: 60rpx;
    height: 60rpx;
    border: 0rpx solid #fff;
    border-radius: 60rpx;
}
.container .header .pic .userinfo {
    width: 580rpx;
    font-size: 0.8rem;
    margin-left: 15rpx;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.noteoper {
    width: 700rpx;
    background: #fff;
    height: 400rpx;
    border-radius: 25rpx;
    margin-top: -180rpx;
    display: flex;
    flex-direction: column;

    align-items: center;
}

.notenav {
    width: 600rpx;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 60rpx;
}

.noteoper .money {
    font-size: 28px;
    font-weight: bold;
    margin-top: 100rpx;
    color: #39b54a;
}

.notenav .notebtn {
    width: 180rpx;
    background: red;
    color: #fff;
    font-size: 12px;
    height: 80rpx;
    border-radius: 35rpx;
    display: flex;
    justify-content: center;
    align-items: center;
}

.container .list {
    width: 700rpx;
    margin-top: 20rpx;
    margin-bottom: 20rpx;
    background: #fff;
    border-radius: 25rpx;
}
.container .list .item {
    padding-top: 35rpx;
    padding-bottom: 35rpx;
    font-size: 14px;
    border-bottom: 0rpx solid #efefef;
    padding-left: 20rpx;
    color: #747474;
    display: flex;
    flex-direction: row;
}
.container .list .item image {
    width: 55rpx;
    height: 55rpx;
    margin-right: 10rpx;
}

.imagePathBox {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
}
.shengcheng {
    width: 80%;
    height: 80%;
    position: fixed;
    top: 50rpx;
    left: 50%;
    margin-left: -40%;
    z-index: 10;
}
.baocun {
    display: block;
    width: 80%;
    height: 80rpx;
    padding: 0;
    line-height: 80rpx;
    text-align: center;
    position: fixed;
    bottom: 50rpx;
    left: 10%;
    background: #ffe200;
    color: #333;
    font-size: 32rpx;
    border-radius: 44rpx;
}
button[class='baocun']::after {
    border: 0;
}
</style>
